package com.jsmy.marketing.fg;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jsmy.marketing.R;
import com.jsmy.marketing.activity.RankListActivity;
import com.jsmy.marketing.activity.ResouceDetailActivity;
import com.jsmy.marketing.activity.ResouceMoreActivity;
import com.jsmy.marketing.adapter.HomeFragmentAdapter;
import com.jsmy.marketing.adapter.HomeFragmentZAdapter;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.bean.AppTJListBean;
import com.jsmy.marketing.bean.AppZLListBean;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.utils.L;
import com.jsmy.marketing.utils.ShareUtil;
import com.jsmy.marketing.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/17.
 * Time: 17:48.
 * To change for file | settings | file and code templates.
 */

public class HomeFragment extends BaseFragment implements UMShareListener {

    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_paihang)
    TextView tvPaihang;
    @BindView(R.id.m_rcycler)
    RecyclerView mRcycler;
    HomeFragmentAdapter adapter;
    List<AppTJListBean.DataBean.ListBean> list;

    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.z_rcycler)
    RecyclerView zRcycler;
    HomeFragmentZAdapter zAdapter;
    List<AppZLListBean.DataBean.ListBean> listZ;

    String shareId;
    String sharePt;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @Override
    protected int resLayout() {
        return R.layout.fg_home_layout;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        tvTime.setText("(" + format.format(new Date()) + ")");
        tvPaihang.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        adapter = new HomeFragmentAdapter();
        mRcycler.setLayoutManager(new LinearLayoutManager(getContext()));
        mRcycler.setAdapter(adapter);

        tvMore.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        zAdapter = new HomeFragmentZAdapter();
        zRcycler.setLayoutManager(new LinearLayoutManager(getContext()));
        zRcycler.setAdapter(zAdapter);
        zAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.img_tip:
                        shareId = listZ.get(position).getId();
                        ShareUtil.shareWeb(getActivity(), listZ.get(position).getUrl(), listZ.get(position).getTitle(), listZ.get(position).getNote(), HomeFragment.this);
                        break;
                    case R.id.linear_bar:
                        Bundle b = new Bundle();
                        b.putString("share", "Y");
                        b.putString("id", listZ.get(position).getId());
                        b.putString("title", listZ.get(position).getTitle());
                        b.putString("url", listZ.get(position).getUrl());
                        b.putString("note", listZ.get(position).getNote());
                        openActivity(ResouceDetailActivity.class, b);
                        break;
                }
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                requestZL();
            }
        });
        update();
    }

    /**
     * 获取资料 更新资料
     */
    public void requestZL() {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        mPresenter.loadData(RetrofitManager.getInstance().getAppZlList(map), Link.APP_ZL_LIST);
    }

    /**
     * 初始化数据，首页数据更新。
     */
    @Override
    public void update() {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        mPresenter.loadData(RetrofitManager.getInstance().getAppTJList(map), Link.APP_TJ_LIST);
//        requestZL();
        refreshLayout.autoRefresh();
    }

    @OnClick({R.id.tv_paihang, R.id.tv_more})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_paihang://排行榜
                openActivity(RankListActivity.class);
                break;
            case R.id.tv_more://更多
                openActivity(ResouceMoreActivity.class);
                break;
        }
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA platform) {
        switch (platform) {
            case QQ:
                sharePt = "QQ";
                break;
            case QZONE:
                sharePt = "QZONE";
                break;
            case SINA:
                sharePt = "SINA";
                break;
            case WEIXIN:
                sharePt = "WEIXIN";
                break;
            case WEIXIN_CIRCLE:
                sharePt = "WEIXIN_CIRCLE";
                break;
            case SMS:
                sharePt = "SMS";
                break;
            case EMAIL:
                sharePt = "EMAIL";
                break;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("id", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("planid", "");
        map.put("pt", sharePt);
        map.put("obj", "");
        mPresenter.loadData(RetrofitManager.getInstance().saveFxInfo(map), Link.APP_SAVE_FX_INFO);
        L.e(TAG,""+platform.toSnsPlatform().mShowWord);
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        ToastUtil.showShort(getContext(), "分享失败，" + throwable.getMessage());
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        ToastUtil.showShort(getContext(), "分享取消");
    }

    @Override
    public void refreshData(Object response, String position) {
        switch (position) {
            case Link.APP_TJ_LIST:
                list = ((AppTJListBean) response).getData().getList();
                adapter.replaceData(list);
                break;
            case Link.APP_ZL_LIST:
                listZ = ((AppZLListBean) response).getData().getList();
                zAdapter.replaceData(listZ);
                break;
            case Link.APP_SAVE_FX_INFO:
                ToastUtil.showShort(getContext(), "分享成功！");
                break;

        }
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        }
    }
}
